$(document).ready(function() {
	showDoctor();
	showSxks();
	showUser();
    $('#ss').searchbox({
        searcher:function(value,name){
        	showUser(value);
        },
        menu:'#mm',
        prompt:'请输入姓名'
    });
});

function showDoctor() {
	$('#lrys').combobox({
		url : "login_showDoctorList.do",
		valueField : 'id',
		textField : 'name'
	});
}

function showSxks() {
	$('#sxks').combobox({
		url : "ksmanage_showKsList.do",
		valueField : 'id',
		textField : 'dname'
	});
}
function showUser(name) {
	$("#patient")
			.datagrid(
					{
						url : "login_showAllPatient.do?name="+name,
						rownumbers : true,
						pagination : true,
						rownumber : true,
						fitColumns : true,
						striped : true,
						collapsible : true,
						remoteSort : false,
						nowrap : false,
						checkOnSelect : false,
						singleSelect : true,
						loadMsg : '数据加载中，请稍等...',
						columns : [ [
								{
									field : 'name',
									title : '姓名',
									width : 100,
									align : 'center'
								},
								{
									field : 'age',
									title : '年龄',
									width : 100,
									align : 'center'
								},
								{
									field : 'mobile',
									title : '手机号码',
									width : 100,
									align : 'center'
								},
								{
									field : 'email',
									title : '邮箱',
									width : 100,
									align : 'center'
								},
								{
									field : 'update',
									title : '操作',
									width : 100,
									align : 'center',
									formatter : function(value, rowData,
											rowIndex) {
										return "<a href='javascript:void(0);' onclick='writePatient("
												+ parseInt(rowData.id)
												+ ")'>录入病例</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:void(0);' onclick='showPatient("
												+ parseInt(rowData.id)
												+ ","
												+ '"'
												+ rowData.name
												+ '"'
												+ ")'>查看历史病例</a>";
									}
								} ] ]
					});
}

function showPatient(uid,name) {
	$("#showPatient").attr("title",name+"就诊历史表");
	$("#showPatient").datagrid({
		url : "patientmanage_showOnePatient.do?uid="+uid,
		rownumbers : true,
		pagination : true,
		rownumber : true,
		fitColumns : true,
		striped : true,
		collapsible : true,
		remoteSort : false,
		nowrap : false,
		checkOnSelect : false,
		singleSelect : true,
		loadMsg : '数据加载中，请稍等...',
		columns : [ [ {
			field : 'pdesc',
			title : '病例诊断',
			width : 100,
			align : 'center'
		}, {
			field : 'psuggest',
			title : '医生建议',
			width : 100,
			align : 'center'
		}, {
			field : 'pks',
			title : '所属科室',
			width : 100,
			align : 'center'
		}, {
			field : 'pcontent',
			title : '检查内容',
			width : 100,
			align : 'center'
		}, {
			field : 'presult',
			title : '检查结果',
			width : 100,
			align : 'center'
		}, {
			field : 'dateFormmat',
			title : '就诊时间',
			width : 100,
			align : 'center'
		},{
			field : 'pdoctor',
			title : '所属医生',
			width : 100,
			align : 'center'
		}] ]
	});
}

function _save() {
	var suggest = $("#suggest").val();
	var desc = $("#desc").val();
	var uid = $("#uid").val();
	var lrys = $("#lrys").combobox("getText");
	var sxks = $("#sxks").combobox("getText");
	var content = $("#content").val();
	var result = $("#result").val();
	if (uid == "") {
		$.messager.alert("提示", "错误数据");
		return;
	}
	if (suggest.trim() == "" || desc.trim() == "" || lrys.trim() == ""
			|| sxks.trim() == "" || content.trim() == "" || result.trim() == "") {
		$.messager.alert("提示", "请填写完整");
	}
	$.post("patientmanage_save.do", {
		"suggest" : suggest,
		"desc" : desc,
		"uid" : uid,
		"lrys" : lrys,
		"sxks" : sxks,
		"content" : content,
		"result" : result
	}, function(data) {
		if (data.success == "success") {
			$.messager.alert("提示", "操作成功");
			showUser();
			$("#add_patient").dialog({
				closed : true
			});
		} else {
			$.messager.alert("提示", "操作失败");
		}
	}, "json");

}

function writePatient(id) {
	$.post("login_showOne.do", {
		"id" : id
	}, function(data) {
		$("#name").text(data.name);
		$("#sex").text(data.sex);
		$("#age").text(data.age);
		$("#mobile").text(data.mobile);
		$("#email").text(data.email);
		$("#address").text(data.address);
		$("#uid").val(data.id);
	}, "json");
	showDialog();
}

function showDialog() {
	$("#add_patient").dialog({
		closed : false
	});
}